package ldh.im.fxbase.util;

import ldh.im.fxbase.richtext.TextItem;
import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Optional;

public class StringUtil {

    private static String LETTERS = "abcdefghijklmnopqrstuvwxyz";

    public static boolean isEmpty(String str) {
        if (str == null || str.equals("")) return true;
        return false;
    }

    public static boolean isNotBlank(String str) {
        return !isEmpty(str);
    }

    public static String getLatelyMessage(String message) {
        Optional<String> firstLineOption = message.lines().findFirst();
        if (firstLineOption.isPresent()) {
            message = firstLineOption.get();
            if (message != null) {
                boolean isLongStr = StringUtil.stringBytesLength(message) > ParamUtil.MESSAGE_SESSION_LATELY_MESSAGE_LENGTH;
                message = StringUtil.subString(message, ParamUtil.MESSAGE_SESSION_LATELY_MESSAGE_LENGTH);
                if(isLongStr) message += "...";

                return message;
            }
        }
        return message;
    }
    /**
     * 根据字节长度获取字符串
     * @param str
     * @param byteLength
     * @return
     */
    public static String subString(String str, int byteLength) {
        if (str == null) return str;
        try {
            StringBuffer result = new StringBuffer();
            int length = 0;
            int emojoIdx = 0;
            for (int i=0, l=str.length(); i<l; i++) {
                String s = str.substring(i, i+1);
                result.append(s);
                if (emojoIdx == 1) {
                    if (s.equals("}")) {
                        length += 4;
                        emojoIdx = 0;
                    } else {
                        continue;
                    }
                }
                if (s.equals(TextItem.SPLIT_STRING)) {
                    emojoIdx++;
                    continue;
                } else if (!s.equals("}")) {
                    length += s.getBytes("utf8").length;
                }
                if (length >= byteLength && emojoIdx == 0) {
                    String r = result.toString();
                    return r;
                }
            }
        } catch (Exception e) {
        }
        return str;
    }

    public static int stringBytesLength(String str) {
        if (str == null) return 0;
        try {
            int length = 0;
            int emojoIdx = 0;
            for (int i=0, l=str.length(); i<l; i++) {
                String s = str.substring(i, i+1);
                if (emojoIdx == 1) {
                    if (s.equals("}")) {
                        length += 3;
                        emojoIdx = 0;
                    } else {
                        continue;
                    }
                }
                if (s.equals(TextItem.SPLIT_STRING)) {
                    emojoIdx++;
                    continue;
                } else if (!s.equals("}")) {
                    length += s.getBytes("utf8").length;
                }
            }
            return length;
        } catch (Exception e) {
        }
        return str.length();
    }

    public static String firstLetter(String str) {
        if (str == null || str.length() < 1) throw new IllegalArgumentException("参数不能为空");
        String firstStr = str.substring(0, 1);
        String[] array = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0));
        String firstLetter = firstStr;
        if (array != null) {
            char c = array[0].charAt(0);
            firstLetter = c + "";
        }
        if (LETTERS.contains(firstLetter.toLowerCase())) {
            return firstLetter.toUpperCase();
        }
        return "#";
    }

    public static boolean isNumber(String str) {
        return str.matches("\\d+");
    }
}
